class CryptoManager {
  // 加密数据
  static async encrypt(data: string, password: string): Promise<string> {
    try {
      // 使用微信小程序提供的加密API
      const encryptedData = await new Promise<string>((resolve, reject) => {
        wx.encrypt({
          data: data,
          key: password,
          success: (res) => resolve(res.encryptedData),
          fail: reject
        });
      });
      return encryptedData;
    } catch (error) {
      console.error('加密失败:', error);
      throw error;
    }
  }

  // 解密数据
  static async decrypt(encryptedData: string, password: string): Promise<string> {
    try {
      // 使用微信小程序提供的解密API
      const decryptedData = await new Promise<string>((resolve, reject) => {
        wx.decrypt({
          encryptedData: encryptedData,
          key: password,
          success: (res) => resolve(res.decryptedData),
          fail: reject
        });
      });
      return decryptedData;
    } catch (error) {
      console.error('解密失败:', error);
      throw error;
    }
  }

  // 生成随机密码
  static generatePassword(length: number = 8): string {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let password = '';
    for (let i = 0; i < length; i++) {
      password += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return password;
  }

  // 验证密码强度
  static validatePassword(password: string): boolean {
    // 密码至少8位，包含大小写字母和数字
    const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/;
    return regex.test(password);
  }

  // 生成文件加密密钥
  static generateFileKey(): string {
    return this.generatePassword(16);
  }

  // 加密文件
  static async encryptFile(filePath: string, password: string): Promise<string> {
    try {
      // 读取文件内容
      const fileContent = await new Promise<ArrayBuffer>((resolve, reject) => {
        const fs = wx.getFileSystemManager();
        fs.readFile({
          filePath,
          success: (res) => resolve(res.data as ArrayBuffer),
          fail: reject
        });
      });

      // 转换为字符串
      const content = new TextDecoder().decode(fileContent);

      // 加密内容
      const encryptedContent = await this.encrypt(content, password);

      // 保存加密后的文件
      const encryptedFilePath = `${filePath}.encrypted`;
      await new Promise<void>((resolve, reject) => {
        const fs = wx.getFileSystemManager();
        fs.writeFile({
          filePath: encryptedFilePath,
          data: encryptedContent,
          encoding: 'utf8',
          success: resolve,
          fail: reject
        });
      });

      return encryptedFilePath;
    } catch (error) {
      console.error('文件加密失败:', error);
      throw error;
    }
  }

  // 解密文件
  static async decryptFile(encryptedFilePath: string, password: string): Promise<string> {
    try {
      // 读取加密文件内容
      const encryptedContent = await new Promise<string>((resolve, reject) => {
        const fs = wx.getFileSystemManager();
        fs.readFile({
          filePath: encryptedFilePath,
          encoding: 'utf8',
          success: (res) => resolve(res.data as string),
          fail: reject
        });
      });

      // 解密内容
      const decryptedContent = await this.decrypt(encryptedContent, password);

      // 保存解密后的文件
      const decryptedFilePath = encryptedFilePath.replace('.encrypted', '');
      await new Promise<void>((resolve, reject) => {
        const fs = wx.getFileSystemManager();
        fs.writeFile({
          filePath: decryptedFilePath,
          data: decryptedContent,
          encoding: 'utf8',
          success: resolve,
          fail: reject
        });
      });

      return decryptedFilePath;
    } catch (error) {
      console.error('文件解密失败:', error);
      throw error;
    }
  }
}

export default CryptoManager; 